﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Model;
using SDLITS.Common;
using MySoft.Data;

namespace SDLITS.Web.Basic
{
    public partial class DepartmentManage : CommonBase
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNew.OnClientClick = Window1.GetShowReference("DepartmentChange.aspx", "新增道口");
                BindGrid();
            }
        }
        private void BindGrid()
        {
            List<V_DepartUser> items = new List<V_DepartUser>();
            GetItems(items,0);
            Grid1.DataSource = items;
            Grid1.DataBind();
        }
        protected void GetItems(List<V_DepartUser> lists,int top)
        {
            List<V_DepartUser> itemsall = SDDbSession.Default.From<V_DepartUser>().ToList();
            List<V_DepartUser> nitems = itemsall.FindAll(p => p.DirID == top);
            foreach (var item in nitems)
            {
                lists.Add(item);
                int count=itemsall.FindAll(p => p.DirID == item.DeptID).Count;
                if (count > 0)
                {
                    GetItems(lists,item.DeptID);
                }
            }
        }
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                if (SDDbSession.Default.From<Department>().Where(Department._.DirID == id).Count() > 0)
                {
                    Alert.Show("该部门下面还有子部门，不能删除！");
                    return;
                }
                SDDbSession.Default.Delete<Department>(Department._.DeptID == id);
                BindGrid();

            }
        }
    }
    
}
